#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

//把细节细化

// 实际网络通信类
typedef struct _NetworkClient {
    void (*connect)(struct _NetworkClient*);
    void (*sendData)(struct _NetworkClient*, const char*);
    void (*disconnect)(struct _NetworkClient*);
} NetworkClient;

void connect(NetworkClient* client) {
    printf("Connecting to server...\n");
}

void sendData(NetworkClient* client, const char* data) {
    printf("Sending data: %s\n", data);
}

void disconnect(NetworkClient* client) {
    printf("Disconnecting from server...\n");
}

// 网络代理类
typedef struct _NetworkProxy {
    NetworkClient* realClient;
    void (*send)(struct _NetworkProxy*, const char*);
} NetworkProxy;

void send(NetworkProxy* proxy, const char* data) {
    assert(proxy != NULL);
    assert(proxy->realClient != NULL);
    proxy->realClient->connect(proxy->realClient);
    proxy->realClient->sendData(proxy->realClient, data);
    proxy->realClient->disconnect(proxy->realClient);
}

// 主函数
int main() {
    // 创建实际网络客户端实例
    NetworkClient realClient = {
        .connect = connect,
        .sendData = sendData,
        .disconnect = disconnect
    };

    // 创建网络代理实例
    NetworkProxy networkProxy = {
        .realClient = &realClient,
        .send = send
    };

    // 使用网络代理
    networkProxy.send(&networkProxy, "Hello, server!");

    return 0;
}
